package com.grape.action.base;

import java.io.File;
import java.io.FileInputStream;

import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;

import com.grape.action.common.BaseAction;
import com.grape.domain.base.Area;
import com.grape.domain.base.SubArea;
import com.grape.service.base.SubAreaService;

@Namespace("/subArea")
@ParentPackage("json-default")
@Scope("prototype")
@Controller
public class SubAreaAction extends BaseAction<SubArea>{
	private static final long serialVersionUID = 1L;

	
	
	@Action(value="batchImport")
	public void batchImport() throws Exception {
		// 获取这个文件使用.xls解析 数据入库
		HSSFWorkbook hssfWorkbook = new HSSFWorkbook(new FileInputStream(file));
		HSSFSheet sheetAt = hssfWorkbook.getSheetAt(0);
		
		ss.saveBatch(sheetAt);
	}
	
	@Action(value="findAll",results=@Result(name="success",type="json"))
	public String findAll() {
		// 基本分页
		Pageable pageable = new PageRequest(page-1, rows);
		
		Page<SubArea> page = ss.findAll(model,pageable);
		pushPageDataToValueStack(page);
		
		return SUCCESS;
	}
	
	@Autowired
	private SubAreaService ss;
	
	private File file;
	public void setFile(File file) {
		this.file = file;
	}
	
}
